package dcedit;

// Import Java SDK Classes
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

// Import Application Classes
import dcedit.factories.*;
import dcedit.util.Constants;
import dcedit.util.Util;

/**
 * The DEdit class<br/>
 * This class is launched from the command line.
 * @author dca
 */
public class DCEdit extends JFrame {
   /** Serializable constant */
   public static final long serialVersionUID = 1283715237526L;

   /** Constructor */
   public DCEdit() {
      super(Constants.TITLE_TEXT);
      Util.setComponentSizes(Constants.FILE_CHOOSER, Constants.FILE_CHOOSER_SIZE, true, false);
   } // END constructor

   /**
    * The main class method<br/>
    * This method coordinates the Substance UI look and feel with the application,
    * create an instance of DCEdit and calls it's main method.
    * @param ARGS The command line arguments.<br/>
    *        Known arguments:<br/>
    *        (None)
    */
   public static void main(final String[] ARGS) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            // Set Look and Feel Decorated.
            setDefaultLookAndFeelDecorated(true);

            // Read color configuration from ser file.
            // If an ser file doesn't exist, defaults will be supplied.
            Util.readDCEditColorConfig();

            // Set other (custom) look and feel styles.
            UIManager.put("ToolTip.font",Constants.TOOLTIP_FONT);

            // Launch the application
            DCEdit app = new DCEdit();
            app.main();
         } // END run
      });
   } // END static main.

   /**
    * The main method<br/>
    * This method is a separation of responsibility from the main class method.<br/>
    * It displays a splash screen, calls the application container initializer,
    * registers the window listener, invokes the utilities to read the config files (if any,)
    * packs, centers, and displays the Application.
    */
   public void main() {
      new Splash(this);
      this.init();
      this.pack();
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(DCEdit.DO_NOTHING_ON_CLOSE);
      this.addWindowListener(ActionFactory.createWindowActionListener());

      // Read in font configuration from ser file.
      // If an ser file doesn't exist, defaults will be supplied.
      Util.readDCEditFontConfig();

      // Requires font styles to be previously set.
      Util.createCurrentFont(Util.getSelectedTextPane());
      Util.getSelectedTextPane().setAutoIndentEnabled(Constants.AUTO_INDENT_BTN.isSelected());

      // Display the application.
      this.setVisible(true);
   } // END main

   /**
    * The init method<br/>
    * This method invokes the methods to create the menu bar and content pane.<br/>
    * This is done at application startup.
    * @see MenuFactory#createAppMenuBar(DCEdit)
    * @see PanelFactory#createAppPanel()
    */
   public void init() {
      this.setJMenuBar(MenuFactory.createAppMenuBar(this));
      this.getContentPane().add(PanelFactory.createAppPanel());
   } // END init
} // END class DEdit